package common

import (
	"encoding/json"
	"sale-admin/internal/app/web/schema"
	"strconv"
	"unicode"
)

type Goods struct{}

// 多规格商品老到新  兼容之前的字段，取出时转新的字段
func (Goods) MultiGoodsOldToNew(countData string) (data []schema.MultiGoodsNew) {
	var OldMultiGoods []schema.MultiGoodsOld
	json.Unmarshal([]byte(countData), &OldMultiGoods)
	if len(OldMultiGoods) > 0 {
		var CountData []schema.MultiGoodsNew
		for _, v := range OldMultiGoods {
			var v1 schema.MultiGoodsNew
			// v1.GoodsID = strconv.Itoa(v.GoodsID)
			v1.GoodsID = v.GoodsID
			v1.GoodsImage = v.GoodsImage
			v1.Price = v.Price
			CountData = append(CountData, v1)
		}
		return CountData
	} else {
		var CountData []schema.MultiGoodsNew
		json.Unmarshal([]byte(countData), &CountData)
		return CountData
	}
}

// 多规格商品新到老  兼容之前的字段，存入时转老版本字段
func (Goods) MultiGoodsNewToOld(countData string) (data []schema.MultiGoodsOld) {
	var NewMultiGoods []schema.MultiGoodsNew
	json.Unmarshal([]byte(countData), &NewMultiGoods)
	if len(NewMultiGoods) > 0 {
		var CountData []schema.MultiGoodsOld
		for _, v := range NewMultiGoods {
			var v1 schema.MultiGoodsOld
			// v1.GoodsID, _ = strconv.Atoi(v.GoodsID)
			v1.GoodsID = v.GoodsID
			v1.GoodsImage = v.GoodsImage
			v1.Price = v.Price
			CountData = append(CountData, v1)
		}
		return CountData
	} else {
		var CountData []schema.MultiGoodsOld
		json.Unmarshal([]byte(countData), &CountData)
		return CountData
	}
}

func (Goods) StrToUnicode(str string) string {
	DD := []rune(str) //需要分割的字符串内容，将它转为字符，然后取长度。
	finallStr := ""
	for i := 0; i < len(DD); i++ {
		if unicode.Is(unicode.Scripts["Han"], DD[i]) {
			textQuoted := strconv.QuoteToASCII(string(DD[i]))
			finallStr += textQuoted[1 : len(textQuoted)-1]
		} else {
			finallStr += string(DD[i])
		}
	}
	return finallStr
}
